package org.komarichin.feed;

import com.sun.syndication.feed.synd.*;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;

import java.util.ArrayList;
import java.util.List;

/**
 * User: Mykhaylo Komarichyn
 * Date: 26.02.2010
 * Time: 15:43:10
 */
public class FeedWriter implements IFeedWriter {
    /**
     * {@inheritDoc}
     */
    @Override
    public String getFeeds(String title, String link, String description, String feedType, List<IEntryFeed> entries) throws FeedException {

        List<SyndEntry> publishingEntries = new ArrayList<SyndEntry>();
        for (IEntryFeed e : entries) {
            SyndEntry entry = new SyndEntryImpl();
            SyndContent entryDescription = new SyndContentImpl();

            entry.setPublishedDate(e.getUpdate());
            entry.setTitle(e.getTitle());
            entry.setLink(e.getLink());
            entryDescription.setType("text/html");
            entryDescription.setValue(e.getDescription());
            entry.setDescription(entryDescription);

            publishingEntries.add(entry);
        }


        SyndFeed feed = new SyndFeedImpl();
        feed.setFeedType(feedType);

        feed.setTitle(title);
        feed.setLink(link);
        feed.setDescription(description);

        feed.setEntries(publishingEntries);

        SyndFeedOutput output = new SyndFeedOutput();
        return output.outputString(feed);
    }
}
